<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/4/11 11:52
 * Remark:
 */

namespace App\Service\Approval;

use App\Consts\BaseConsts\BaseConsts;
use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\ResponseFunc;
use App\Func\TokenFuc;
use App\Models\Approval\OldDingTalkApprovalTemplateModel;
use App\ServiceChecker\Approval\ApprovalLegitimacy;
use Illuminate\Http\Exceptions\HttpResponseException;

class OldDingTalkApprovalTemplateService
{
    private ApprovalLegitimacy $_approvalLegitimacy;
    private OldDingTalkApprovalTemplateModel $_oldDingTalkApprovalServiceModel;

    public function __construct()
    {
        $this->_oldDingTalkApprovalServiceModel = new OldDingTalkApprovalTemplateModel();
        $this->_approvalLegitimacy = new ApprovalLegitimacy();
    }

    public function getOldDingTalkApprovalTempSelect($dingTalkApprovalTempSelectType){
        return $this->_oldDingTalkApprovalServiceModel->getOldDingTalkApprovalTempSelect($dingTalkApprovalTempSelectType);
    }

    /**
     * 校验新增审批模板方法
     * @param $args
     */
    private function checkerAddOldDingTalkApprovalTemp($args){
        $this->_approvalLegitimacy->verifyCompanyIsExists($args['companyId']);
        $this->_approvalLegitimacy->verifyCompanyApprovalTemplateIsExists($args['companyId'],$args['templateCode']);
    }

    /**
     * 新增钉钉审批模板
     * @param $talkApprovalTempData
     * @return mixed
     */
    public function addOldDingTalkApprovalTemp($talkApprovalTempData): mixed
    {
        $currentInfo = TokenFuc::getCurrentStaffInfo();
        $companyId = $talkApprovalTempData['company_id'] ?? $currentInfo['company_id'];
        // 校验
        $checkerArgs = [
            'companyId' => $companyId,
            'templateCode' => $talkApprovalTempData['ding_talk_approval_template_code']
        ];
        $this->checkerAddOldDingTalkApprovalTemp($checkerArgs);
        $time = time();
        $talkApprovalTempData['company_id'] = $companyId;
        $talkApprovalTempData['status'] = BaseConsts::STATUS_NORMAL;
        $talkApprovalTempData['created_at'] = $time;
        $talkApprovalTempData['updated_at'] = $time;
        $res =  $this->_oldDingTalkApprovalServiceModel->addOldDingTalkApprovalTemp($talkApprovalTempData);
        if(!$res){
            throw new HttpResponseException(ResponseFunc::returnJson(
                'error',
                null,
                '新增钉钉审批模板错误',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
        return $res;
    }

    /**
     * 校验删除钉钉审批模板的方法
     * @param $args
     */
    public function checkerDelOldDingTalkApprovalTemp($args){
        $this->_approvalLegitimacy->verifyApprovalTemplateIsExists($args['talkApprovalTempId']);
    }

    public function delOldDingTalkApprovalTemp($talkApprovalTempId)
    {
        // 数据校验
        $this->checkerAddOldDingTalkApprovalTemp([
            'talkApprovalTempId' => $talkApprovalTempId
        ]);
        $res = $this->_oldDingTalkApprovalServiceModel->editOldDingTalkApprovalTemp([
            'ding_talk_approval_template_id' => $talkApprovalTempId,
            'status' => BaseConsts::STATUS_DELETE
        ]);
        if(!$res){
            throw new HttpResponseException(ResponseFunc::returnJson(
                'error',
                null,
                '删除钉钉审批模板失败',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }

    /**
     * 获取钉钉审批列表
     * @param $pageSize
     * @param $filters
     * @return mixed
     */
    public function getOldDingTalkApprovalTempList($pageSize,$filters){
        $query = $this->_oldDingTalkApprovalServiceModel->select([
            'ding_talk_approval_template_id',
            'company_id',
            'ding_talk_approval_template_id as key',
            'ding_talk_approval_template_name',
            'ding_talk_approval_template_code',
        ])
            ->where('status',BaseConsts::STATUS_NORMAL);
        if($filters){
            $filters = json_decode($filters,true);
            if(isset($filters['ding_talk_approval_template_name']) && $filters['ding_talk_approval_template_name']){
                $query = $query->where('ding_talk_approval_template_name','LIKE',"%".$filters['ding_talk_approval_template_name']."%");
            }
        }
        return $this->_oldDingTalkApprovalServiceModel->getOldDingTalkApprovalTempList($query,$pageSize);
    }

    public function getOldDingTalkApprovalTempInfo($approvalTempId){
        return $this->_oldDingTalkApprovalServiceModel->getOldDingTalkApprovalTempInfo($approvalTempId,[
            'ding_talk_approval_template_id',
            'ding_talk_approval_template_code',
            'ding_talk_approval_template_name',
            'ding_talk_approval_template_type',
        ]);
    }

}